#include "widget.h"
#include "ui_widget.h"
#include "config.h"
#include <QtDebug>
#include <QMouseEvent>
#include <QTimer>
#include "windows.h"
#include "geometry_vector.h"
#include "mypoint.h"
#include <QCursor>
#include "tool.h"
#include "bullet.h"
void Widget::mouseMoveEvent(QMouseEvent *e)
{
    Q_UNUSED(e);
    if(this->dragTower!=nullptr){
        this->dragTower->SetXY(e->x(),e->y());
    }
    //    qDebug()<<e->pos().x()<<" "<<e->pos().y()<<endl;
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{

}

void Widget::mousePressEvent(QMouseEvent *e)
{
    for(auto it=this->Tower_Display.begin();it!=this->Tower_Display.end();++it){
        if((*it)->GetRect().contains(e->pos())){
            if(this->dragTower!=nullptr){
                delete this->dragTower;
            }
            this->dragTower=new Tower((*it)->GetType(),(*it)->GetX(),(*it)->GetY());
            return;
        }
    }
    if(this->dragTower!=nullptr){
        TowerBuilds.push_back(this->dragTower);
        this->dragTower=nullptr;
    }
}

void Widget::processor_Tower_rotate()
{
    for(auto it=TowerBuilds.begin();it!=TowerBuilds.end();++it){
        Geometry_Vector vec_aim;
        float angle_aim;
        MyPoint aim;
        aim.SetXY(this->mapFromGlobal(QCursor().pos()).x(),this->mapFromGlobal(QCursor().pos()).y());
        MyPoint pole;
        pole.SetXY((*it)->GetX(),(*it)->GetY());
        vec_aim.SetXY(aim.x()-pole.x(),aim.y()-pole.y());
        angle_aim = Tool::vector_to_angle(vec_aim);
        if(abs(angle_aim - (*it)->GetAngle()) > (*it)->Get_velocity_rotation()/50){
            if(abs(angle_aim - (*it)->GetAngle()) <= 180){
                (*it)->SetAngle((*it)->GetAngle()
                                + Tool::sign(angle_aim - (*it)->GetAngle())//这是一个符号函数，算方向的
                                * (*it)->Get_velocity_rotation()/50);

            }else{
                (*it)->SetAngle((*it)->GetAngle()
                                - Tool::sign(angle_aim - (*it)->GetAngle())//这是一个符号函数，算方向的
                                * (*it)->Get_velocity_rotation()/50);

            }
            if((*it)->GetAngle()>=360){
                (*it)->SetAngle((*it)->GetAngle()-360);
            }
            if((*it)->GetAngle()<0){
                (*it)->SetAngle((*it)->GetAngle()+360);
            }
            (*it)->Set_Angle_Ready(false);
        }else {
            (*it)->SetAngle(angle_aim);
            (*it)->Set_Angle_Ready(true);
        }
    }
}

void Widget::processor_Tower_fire()
{
    for(auto it=TowerBuilds.begin();it!=TowerBuilds.end();++it){
        (*it)->FireCoolDown();
        if((*it)->IfFireReady()){
            qDebug()<<"fire"<<endl;
            this->_BulletsList.push_back(new Bullet((*it)));
            (*it)->ResetCoolDown();
        }
    }

}

Widget::Widget(QWidget *parent):QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);
    setMouseTracking(true);
    setWindowFlags(Qt::WindowCloseButtonHint);
    setFixedSize(MAINWINDOW_WIDTH, MAINWINDOW_HEIGHT);
    this->dragTower=NULL;
    this->init();
}

void Widget::init()
{
    this->mouseLeftPressWithTower=false;
    Tower_Display.push_back(new Tower("attacker1",ATTACKER_WIDTH/2+LEFT, MAINWINDOW_HEIGHT-DOWN-ATTACKER_HEIGHT));
    Tower_Display.push_back(new Tower("attacker2",ATTACKER_WIDTH+ATTACKER1_WIDTH/2+LEFT, MAINWINDOW_HEIGHT-DOWN-ATTACKER1_HEIGHT));
    Tower_Display.push_back(new Tower("attacker3",ATTACKER_WIDTH+ATTACKER1_WIDTH+ATTACKER2_WIDTH/2+LEFT,MAINWINDOW_HEIGHT-DOWN-ATTACKER2_HEIGHT));
    Tower_Display.push_back(new Tower("attacker4",ATTACKER_WIDTH+ATTACKER1_WIDTH+ATTACKER2_WIDTH+ATTACKER3_WIDTH/2+LEFT,MAINWINDOW_HEIGHT-DOWN-ATTACKER3_HEIGHT));
    Tower_Display.push_back(new Tower("attacker5",ATTACKER_WIDTH+ATTACKER1_WIDTH+ATTACKER2_WIDTH+ATTACKER3_WIDTH+ATTACKER4_WIDTH+LEFT, MAINWINDOW_HEIGHT-DOWN-ATTACKER4_HEIGHT));
    timer=new QTimer;
    timer->setInterval(20);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start();
    this->GenerateTimer=new QTimer;
    this->GenerateTimer->setInterval(1000);
    connect(this->GenerateTimer,&QTimer::timeout,this,&Widget::GenerateEnemy);
    this->GenerateTimer->start();
}

void Widget::GenerateEnemy()
{

    int m=qrand()%10;

    int posY=qrand()%500;
    enemy* enemy_temp=nullptr;

    switch (m) {
    case 1:enemy_temp=new enemy(50,posY,EnemyType::EnemyType1);break;
    case 2:enemy_temp=new enemy(50,posY,EnemyType::EnemyType2);break;
    case 3:enemy_temp=new enemy(50,posY,EnemyType::EnemyType3);break;
    case 4:enemy_temp=new enemy(50,posY,EnemyType::EnemyType4);break;
    case 5:enemy_temp=new enemy(50,posY,EnemyType::EnemyType5);break;
    case 6:enemy_temp=new enemy(50,posY,EnemyType::EnemyType6);break;
    case 7:enemy_temp=new enemy(50,posY,EnemyType::EnemyType7);break;
    case 8:enemy_temp=new enemy(50,posY,EnemyType::EnemyType8);break;
    case 9:enemy_temp=new enemy(50,posY,EnemyType::EnemyType9);break;
    case 10:enemy_temp=new enemy(50,posY,EnemyType::EnemyType10);break;
    default:
        break;
    }
    if(enemy_temp!=nullptr){
        Blood* blood_temp=new Blood;
        blood_temp->SetEnemy(enemy_temp);
        enemy_temp->SetObject(blood_temp);
        this->_BloodList.push_back(blood_temp);
        this->EnemyList.push_back(enemy_temp);
    }
}

Widget::~Widget()
{
    delete ui;
    delete dragTower;
    for(auto it=Tower_Display.begin();it!=Tower_Display.end();++it){
        delete (*it);
    }
    for(auto it=TowerBuilds.begin();it!=TowerBuilds.end();++it){
        delete (*it);
    }

    for(auto it=EnemyList.begin();it!=EnemyList.end();++it){
        delete (*it);
    }

    for(auto it=this->_BulletsList.begin();it!=this->_BulletsList.end();++it){
        delete (*it);
    }
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPixmap x;
    x.load(":/pictures/background/backGround1.png");
    QPainter p(this);
    p.drawPixmap(0,0,x);

    for(auto it=this->Tower_Display.begin();it!=Tower_Display.end();++it){
        p.translate((*it)->GetX(),(*it)->GetY());
        p.rotate(180);
        p.drawPixmap(-(*it)->GetWidth()/2,-(*it)->GetHeight(),(*it)->GetWidth(),(*it)->GetHeight(),(*it)->GetPix());
        p.rotate(-180);
        p.translate(-(*it)->GetX(),-(*it)->GetY());
        //        qDebug()<<(*it)->GetX()<<" "<<(*it)->GetY()<<endl;
    }

    for(auto it=this->Tower_Display.begin();it!=Tower_Display.end();++it){
        p.drawRect((*it)->GetRect());
    }

    if(this->dragTower!=nullptr){
        p.translate(this->dragTower->GetX(),this->dragTower->GetY());
        p.rotate(180);
        p.drawPixmap(-this->dragTower->GetWidth()/2,-this->dragTower->GetHeight()/2,this->dragTower->GetWidth(),this->dragTower->GetHeight(),this->dragTower->GetPix());
        p.rotate(-180);
        p.translate(-this->dragTower->GetX(),-this->dragTower->GetY());
    }
    for(auto it=TowerBuilds.begin();it!=TowerBuilds.end();++it){
        p.translate((*it)->GetX(),(*it)->GetY());
        p.rotate((*it)->GetAngle());
        p.drawPixmap(-(*it)->GetWidth()/2,-(*it)->GetHeight()/2,(*it)->GetWidth(),(*it)->GetHeight(),(*it)->GetPix());
        p.rotate(-(*it)->GetAngle());
        p.translate(-(*it)->GetX(),-(*it)->GetY());
    }

    processor_Tower_rotate();
    processor_Tower_fire();
    qDebug()<<"---------0-------"<<endl;
    for(auto One_enemy=this->EnemyList.begin();One_enemy!=this->EnemyList.end();++One_enemy){
        for(auto One_Bullet=this->_BulletsList.begin();One_Bullet!=this->_BulletsList.end();++One_Bullet){
            if((*One_enemy)->GetDelete()){
                break;
            }
            if((*One_Bullet)->GetDelete()){
                continue;
            }
            if((*One_enemy)->GetRect().intersects((*One_Bullet)->GetRect())){
                (*One_enemy)->DeLife((*One_Bullet)->GetDamage());
                (*One_Bullet)->SetDelete();
                if((*One_enemy)->GetLife()<=0){
                    (*One_enemy)->SetDelete();
                }
            }
        }
    }
    qDebug()<<"--------1------"<<endl;
    for(int i=0;i<this->EnemyList.size();++i){
        if((!this->EnemyList[i]->CheckPosition())||this->EnemyList[i]->GetDelete()){
            delete this->EnemyList[i];
            this->EnemyList.removeAt(i);
            i--;
            continue;
        }
        this->EnemyList[i]->SetXY(this->EnemyList[i]->GetX()+ this->EnemyList[i]->Get_velocity()/50, this->EnemyList[i]->GetY());
        p.drawPixmap( this->EnemyList[i]->GetX(), this->EnemyList[i]->GetY(), this->EnemyList[i]->GetWidth(), this->EnemyList[i]->GetHeight(), this->EnemyList[i]->GetPix());
        p.drawRect( this->EnemyList[i]->GetRect());
    }

    for(int i=0;i<this->_BulletsList.size();++i){
        if((!this->_BulletsList[i]->CheckPosition())||this->_BulletsList[i]->GetDelete()){
            delete this->_BulletsList[i];
            this->_BulletsList.removeAt(i);
            i--;
            continue;
        }
        this->_BulletsList[i]->UpdataPosition();
        p.drawPixmap(this->_BulletsList[i]->GetX(),this->_BulletsList[i]->GetY(),this->_BulletsList[i]->GetWidth(),this->_BulletsList[i]->GetHeight(),this->_BulletsList[i]->GetPix());
        p.drawRect(this->_BulletsList[i]->GetRect());
    }
    qDebug()<<"--------2--------"<<endl;

    for(int i=0;i<this->_BloodList.size();++i){
        if(!this->_BloodList[i]->IfExist()){
            delete this->_BloodList[i];
            this->_BloodList.removeAt(i);
            i--;
            continue;
        }
        this->_BloodList[i]->Updata(&p);
    }


}

